﻿using System;
using System.Runtime.InteropServices;
using StockUtil;

namespace StockLib.UserData
{
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct BlockCustomFileHead
    {
        public UInt16 m_wDomainNumber;                          // 板块个数
    }

    // 财务信息数据结构 CStock::dataBasetbl
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct BlockCustomFileDomainPack
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string m_szName;                                 // 板块名称
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
        public string m_szShortName;                            // 板块简称
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
        public string m_szClassName;                            // 分类名称
        public UInt16 m_wStockNumber;                           // 股票个数

        public static void FromInternal(StockBlock domain, ref BlockCustomFileDomainPack domainPack)
        {
            domainPack.m_szName = domain.Name;
            domainPack.m_szShortName = domain.ShortName;
            domainPack.m_szClassName = domain.GroupName;
            domainPack.m_wStockNumber = (UInt16)domain.StockNumber;
        }

        public static void ToInternal(ref BlockCustomFileDomainPack domainPack, StockBlock domain)
        {
            domain.Name = domainPack.m_szName;
            domain.ShortName = domainPack.m_szShortName;
            domain.GroupName = domainPack.m_szClassName;
            domain.StockNumber = domainPack.m_wStockNumber;
        }
    };

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct BlockCustomFileStockPack
    {
        public UInt16 m_wMarket;                                // 股票市场类型
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
        public string m_szLabel;                                // 代码

        public static void FromInternal(Stock stock, ref BlockCustomFileStockPack stockPack)
        {
            stockPack.m_wMarket = (UInt16)stock.Market;
            stockPack.m_szLabel = stock.Code;
        }

        public static void ToInternal(ref BlockCustomFileStockPack stockPack, Stock stock)
        {
            stock.Market = (StockMarket)stockPack.m_wMarket;
            stock.Code = stockPack.m_szLabel;
        }

        public static string GetCodeKey(ref BlockCustomFileStockPack stockPack)
        {
            return ((StockMarket)(stockPack.m_wMarket)).ToString().ToUpper() + stockPack.m_szLabel;
        }

        private static StockMarket ConvertMarketToInternal(UInt16 wMarket)
        {
            const int MARKET_SH = 0X4853;
            const int MARKET_SZ = 0X5A53;

            switch (wMarket)
            {
                case MARKET_SH:
                    return StockMarket.SHSE;

                case MARKET_SZ:
                    return StockMarket.SZSE;
            }

            return StockMarket.Unknown;
        }

    };
}
